package cn.zdsoft.configuration.action;

import cn.zdsoft.configuration.entity.JsonData;
import cn.zdsoft.configuration.entity.Node;
import cn.zdsoft.configuration.service.ZookeeperClient;
import cn.zdsoft.configuration.util.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by wangdongdong on 2018/5/29 10:54.
 */
@RequestMapping("/conf/node")
@Controller
public class NodeAction {

    @Resource
    private ZookeeperClient zookeeperClient;

    @RequestMapping("/list")
    public String list(Model model, String proj, String evn, String searchStr) {
        List<String> projs = zookeeperClient.getAllProjectNodes();
        model.addAttribute("projs", projs);
        if (StringUtils.isNotBlank(proj)) {
            List<String> evns = zookeeperClient.getEvnsByProject(proj);
            model.addAttribute("evns", evns);

            if (StringUtils.isBlank(evn)) {
                evn = evns.get(0);
            }
            List<Node> nodes = zookeeperClient.getNodeByProjectAndEven(proj, evn, searchStr == null ? "" : searchStr);
            model.addAttribute("nodes", nodes);
        }
        model.addAttribute("proj", proj);
        model.addAttribute("evn", evn);
        model.addAttribute("searchStr", searchStr);
        return "home/list";
    }

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @ResponseBody
    public JsonData save(String proj, String evn, Node node) {
        try {
            zookeeperClient.saveNode(proj, evn, node);
            return JsonData.success("保存成功");
        } catch (Exception e) {
            return JsonData.fail(e.getMessage());
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    @ResponseBody
    public JsonData delete(String proj, String evn, String nodeName) {
        try {
            zookeeperClient.deleteNode(proj, evn, nodeName);
            return JsonData.success("删除成功");
        } catch (Exception e) {
            return JsonData.fail(e.getMessage());
        }
    }

    @RequestMapping(value = "/import", method = RequestMethod.POST)
    @ResponseBody
    public JsonData importNode(String proj, String evn, @RequestParam("file") List<MultipartFile> files) {
        try {
            List<String> importFile = new ArrayList<>();
            for (MultipartFile file : files) {
                BufferedReader br = new BufferedReader(new InputStreamReader(file.getInputStream()));
                String inputLine;
                while ((inputLine = br.readLine()) != null) {
                    // Empty or comment?
                    if (inputLine.trim().equals("") || inputLine.trim().startsWith("#")) {
                        continue;
                    }
                    if (inputLine.startsWith("-")) {
                        continue;
                    }
                    // TODO 验证配置格式
                    if (inputLine.contains("=")) {
                        String name = inputLine.substring(0, inputLine.lastIndexOf("="));
                        if (name.contains("=")) {
                            continue;
                        }
                    }
                    importFile.add(inputLine);
                }
                br.close();
            }
            zookeeperClient.importProp(proj, evn, importFile);
            return JsonData.success("导入成功");
        } catch (Exception e) {
            return JsonData.fail(e.getMessage());
        }
    }

}
